package com.enqualcomm.kids.mvp.resetpassword;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.activities.GetMessageActivity;
import com.enqualcomm.kids.view.BasePager;

public class ResetPwdStep2Pager extends BasePager implements OnClickListener {

	private View next_step_btn;
	private TextView username_tv;
	private EditText register_validcode_et;
	private Button get_validcode_btn;

	public ResetPwdStep2Pager(Context context) {
		super(context);
	}

	@Override
	public View initView() {
		View root = View.inflate(ct, R.layout.pager_register_step2, null);
		next_step_btn = root.findViewById(R.id.next_step_btn);
		next_step_btn.setOnClickListener(this);
		get_validcode_btn = (Button) root.findViewById(R.id.get_validcode_btn);
		get_validcode_btn.setOnClickListener(this);
		username_tv = (TextView) root.findViewById(R.id.username_tv);
		
		register_validcode_et = (EditText) root.findViewById(R.id.register_validcode_et);
		register_validcode_et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				String inputValidcode = s.toString().trim();
				if (inputValidcode.matches("\\d{6}")) {
					next_step_btn.setEnabled(true);
				}else {
					next_step_btn.setEnabled(false);
				}
			}
		});
		
		return root;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next_step_btn:
			String validcode = register_validcode_et.getText().toString().trim();
			((GetMessageActivity)ct).nextPage(2,validcode);
			break;
		case R.id.get_validcode_btn:
			// 重新获取验证码
//			get_validcode_btn.setText(ct.getString(R.string.sending));
			get_validcode_btn.setEnabled(false);
			((GetMessageActivity)ct).getValidCodeAgain();
			break;

		default:
			break;
		}
		
	}
	
	@Override
	public void clear(){
		register_validcode_et.setText("");
//		get_validcode_btn.setText(ct.getString(R.string.sending));
		get_validcode_btn.setEnabled(false);
	}

	public void setUsername(String username) {
		//用户的手机号
		username_tv.setText(username);
	}
	
	public void refreshLasttime(int lasttime){
		if (lasttime >0) {
			//修改按钮上显示的时间
//			get_validcode_btn.setText(""+lasttime);
			get_validcode_btn.setText(""+lasttime+"s");
		}else {
			//60秒倒计时完毕，按钮可点击
//			get_validcode_btn.setText("");
//			get_validcode_btn.setEnabled(true);
			get_validcode_btn.setText(ct.getString(R.string.valid_resend));
			get_validcode_btn.setEnabled(true);
		}
	}
}
